package com.ktwlsoft.archivemanage.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * 跨域参数
 */
@Data
@Component
@ConfigurationProperties(prefix = "app.cors")
public class CorsProperty {
	private Boolean enabled;

	private String filterPath;

	private List<String> allowedOrigins;

	private List<String> allowedHeaders;

	private List<String> allowedMethods;

	private Boolean swaggerEnabled;

	@PostConstruct
	public void init(){
		if(enabled == null){
			enabled = false;
		}
		if(filterPath == null || "".equals(filterPath)){
			filterPath = "/**";
		}
		if(allowedOrigins == null || allowedOrigins.size() == 0){
			allowedOrigins = new ArrayList<>();
			allowedOrigins.add("*");
		}
		if(allowedHeaders == null || allowedHeaders.size() == 0){
			allowedHeaders = new ArrayList<>();
			allowedHeaders.add("*");
		}
		if(allowedMethods == null || allowedMethods.size() == 0){
			allowedMethods = new ArrayList<>();
			allowedMethods.add("*");
		}
	}
}
